package bf.control;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import bf.model.FactoryService;
import bf.model.MemberVO;

public class FactoryController extends MultiActionController{
	private FactoryService factoryService;
	public FactoryController(FactoryService factoryService){
		this.factoryService = factoryService;
	}
	public ModelAndView mainView(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		return new ModelAndView("index.mes");
	}
	// 장승 - 운영자(공장관리자 or operator) 화면 
	public ModelAndView goOperator(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		return new ModelAndView("operator.factory");
	}
	// 장승 - 사용자 (공장관리자 or operator) 화면
	public ModelAndView goCeo(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		return new ModelAndView("ceo.factory");
	}
	//해당 랏에 인풋할 원재료 kg양을 입력받아 처리하는 로직 -json으로 뭐 넘기냐
	public ModelAndView setLotInput(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		factoryService.setLotInput(Integer.parseInt(request.getParameter("lotInput")));
		// 랏 입력후 완료시 운영자 페이지로 전환
		return new ModelAndView("lot.factory");
		//return new ModelAndView("JsonView");
	}
	// login 컨트롤러
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response, MemberVO mvo) throws SQLException{
		MemberVO loginVO = factoryService.login(mvo);
		if(loginVO!=null){
			request.getSession().setAttribute("login", mvo);
		}
		return new ModelAndView("JsonView", "lmvo", loginVO); // 어떤 페이지에 리턴???? 타일즈 모르겠어..ㅠㅠ
	}
	//logout 컨트롤러
	public ModelAndView logout(HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws SQLException{
		if(session!=null)
			session.invalidate();
		return new ModelAndView("index.mes");
	}
	// machine정보 가져오는 컨트롤러
	public ModelAndView getMachineInfo(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		return new ModelAndView("JsonView", "mcInfo", factoryService.getMachineInfo());
	}
	// machine 추가하는 컨트롤러
	public ModelAndView updateMachine(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		String mc_id = request.getParameter("mc_id");
		factoryService.updateMachine(mc_id);
		return new ModelAndView("MachineInfo.factory", "mcInfo", factoryService.getMachineInfo());
	}
	// 장승-관리에서 공정 추가 뷰
	public ModelAndView addOperation(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		return new ModelAndView("addOperation.factory");
	}
	// 장승-관리에서 주문 추가 뷰
	public ModelAndView addOrder(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		return new ModelAndView("addOrder.factory");
	}
	// 장승-관리에서 제고 뷰
	public ModelAndView stock(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		return new ModelAndView("stock.factory");
	}
	// 장승-관리에서 lot 뷰
	public ModelAndView lot(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		return new ModelAndView("lot.factory");
	}
	 // 공정 현황 페이지
	public ModelAndView goOperationInfo(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		return new ModelAndView("getOperationInfo.factory", "opMap", factoryService.getOperationInfo());
		//return new ModelAndView("JsonView", "opMap", factoryService.getOperationInfo());
	}
	 // 공정 현황 가져오기
	public ModelAndView getOperationInfo(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		return new ModelAndView("JsonView", "opMap", factoryService.getOperationInfo());
	}
	// 장승-관리에서 금일 공정  뷰
	public ModelAndView dayOperResult(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		return new ModelAndView("dayOperResult.factory", "map", factoryService.dayOperResult());
	}
	//실시간 랏 현황 페이지 가져오기 - 제이슨데이터로 랏 리스트를 보냄
	public ModelAndView elementLotState(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		return new ModelAndView("JsonView","getTotalLotInfo",factoryService.elementLotState());
	}
	public ModelAndView goElementLotState(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		return new ModelAndView("elementLotState.factory","lotList",factoryService.elementLotState());
	}
	// history insert
	public ModelAndView setHistory(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		factoryService.setHistory();
		return new ModelAndView("JsonView");
	}
	// CEO 페이지 차트 정보
			public ModelAndView getRefreshInfoCeo(HttpServletRequest request,HttpServletResponse response) throws SQLException{
				System.out.println(factoryService.getRefreshInfo().toString());
				return new ModelAndView("JsonView", "refreshInfo", factoryService.getRefreshInfo().toString());
			}
	// 누적 현황
	public ModelAndView lotHistory(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		return new ModelAndView("lotHistory.factory", "historyInfo", factoryService.getBf_history());
	}
	// 기계 정보
	public ModelAndView goMachineInfo(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		return new ModelAndView("MachineInfo.factory", "mcInfo", factoryService.getMachineInfo());
	}
	public ModelAndView setCloseToday(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		String st_location = request.getParameter("st_location");
		String s_re_mount = request.getParameter("s_re_mount");
		String member_id = request.getParameter("member_id");
		factoryService.setHistory();
		int re_mount = Integer.parseInt(s_re_mount);
		factoryService.setRequire(re_mount, member_id);
		factoryService.setStock(re_mount, member_id, st_location);
		return new ModelAndView("dayOperResult.factory", "map", factoryService.dayOperResult());
	}
	// history 정보 가져오기
	public ModelAndView getBf_history(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		return new ModelAndView("JsonView", "historyInfo", factoryService.getBf_history());
	}
}
